<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getAddressList } from '@/apis/address'
import { checkOrder, submitOrder } from '@/apis/order'
const router = useRouter()
const route = useRoute()
const addressList = ref([])
const order = ref({})
const personal = ref({})
const remark = ref('') // 备注留言

const selectedAddress = computed(() => addressList.value[0] || {})
const longAddress = computed(() => {
  const region = selectedAddress.value.region
  return region.province + region.city + region.region + selectedAddress.value.detail
})
const mode = computed(() => route.query.mode)
const cartIds = computed(() => route.query.cartIds)
const goodsId = computed(() => route.query.goodsId)
const goodsSkuId = computed(() => route.query.goodsSkuId) 
const goodsNum = computed(() => route.query.goodsNum)

const getAllAddressList = async() => {
  const { data: { list } } = await getAddressList()
  addressList.value = list
}

const getOrderList = async() => {
  // 购物车结算
  if (mode.value === 'cart') {
    const { data } = await checkOrder(mode.value, {
      cartIds: cartIds.value
    })
    order.value = data.order
    personal.value = data.personal
  }
  // 立刻购买结算
  if (mode.value === 'buyNow') {
    const { data } = await checkOrder(mode.value, {
      goodsId: goodsId.value,
      goodsSkuId: goodsSkuId.value,
      goodsNum: goodsNum.value
    })
    order.value = data.order
    personal.value = data.personal
  }
}

const submitOrderList = async() => {
   if (mode.value === 'cart') {
    await submitOrder(mode.value, {
      cartIds: cartIds.value,
      remark: remark.value
    })
  }
  if (mode.value === 'buyNow') {
    await submitOrder(mode.value, {
      goodsId: goodsId.value,
      goodsSkuId: goodsSkuId.value,
      goodsNum: goodsNum.value,
      remark: remark.value
    })
  }
  // eslint-disable-next-line no-undef
  showSuccessToast('支付成功')
  router.replace('/order')
}
onMounted(() => {
  getAllAddressList()
  getOrderList()
})
defineOptions({
  name: 'PayIndex'
})
</script>

<template>
  <div class="pay">
    <van-nav-bar fixed title="订单结算台" left-arrow @click-left="router.go(-1)" />
    <!-- 地址相关 -->
    <div class="address">
        <div class="left-icon">
           <van-icon name="logistics" />
        </div>
 
        <div class="info" v-if="selectedAddress.address_id">
          <div class="info-content">
            <span class="name">{{ selectedAddress.name }}</span>
            <span class="mobile">{{ selectedAddress.phone }}</span>
          </div>
          <div class="info-address">
            {{ longAddress }}
          </div>
        </div>
 
        <div class="info" v-else>
          请选择配送地址
        </div>
 
        <div class="right-icon">
           <van-icon name="arrow" />
        </div>
    </div>
    <!-- 订单明细 -->
    <div class="pay-list" v-if="order.goodsList">
      <div class="list">
        <div class="goods-item" v-for="item in order.goodsList" :key="item.goods_id">
            <div class="left">
              <img :src="item.goods_image" alt="" />
            </div>
            <div class="right">
              <p class="tit text-ellipsis-2">
                {{ item.goods_name }}
              </p>
              <p class="info">
                <span class="count">x{{ item.total_num }}</span>
                <span class="price">¥{{ item.total_pay_price }}</span>
              </p>
            </div>
        </div>
      </div>

      <div class="flow-num-box">
        <span>共 {{ order.orderTotalNum }} 件商品，合计：</span>
        <span class="money">￥{{ order.orderTotalPrice }}</span>
      </div>

      <div class="pay-detail">
        <div class="pay-cell">
          <span>订单总金额：</span>
          <span class="red">￥{{ order.orderTotalPrice }}</span>
        </div>

        <div class="pay-cell">
          <span>优惠券：</span>
          <span>无优惠券可用</span>
        </div>

        <div class="pay-cell">
          <span>配送费用：</span>
          <span v-if="!selectedAddress">请先选择配送地址</span>
          <span v-else class="red">+￥0.00</span>
        </div>
      </div>

      <!-- 支付方式 -->
      <div class="pay-way">
        <span class="tit">支付方式</span>
        <div class="pay-cell">
          <span><van-icon name="balance-o" />余额支付（可用 ¥ {{ personal.balance }} 元）</span>
          <!-- <span>请先选择配送地址</span> -->
          <span class="red"><van-icon name="passed" /></span>
        </div>
      </div>

      <!-- 买家留言 -->
      <div class="buytips">
        <textarea v-model="remark" placeholder="选填：买家留言（50字内）" name="" id="" cols="30" rows="10"></textarea>
      </div>
    </div>
    <!-- 底部提交 -->
    <div class="footer-fixed">
       <div class="left">实付款：<span>￥{{ order.orderTotalPrice }}</span></div>
       <div class="tipsbtn" @click="submitOrderList">提交订单</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.pay {
  padding-top: 46px;
  padding-bottom: 46px;
  :deep(.van-nav-bar__title) {
      font-weight: normal;
  }
  :deep(.van-nav-bar__arrow) {
      color: #333;
  }
}
.address {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 20px;
  font-size: 14px;
  color: #666;
  position: relative;
  background: url(@/assets/border-line.png) bottom repeat-x;
  background-size: 60px auto;
  .left-icon {
    margin-right: 20px;
  }
  .right-icon {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-7px);
  }
}
.goods-item {
  height: 100px;
  margin-bottom: 6px;
  padding: 10px;
  background-color: #fff;
  display: flex;
  .left {
    width: 100px;
    img {
      display: block;
      width: 80px;
      margin: 10px auto;
    }
  }
  .right {
    flex: 1;
    font-size: 14px;
    line-height: 1.3;
    padding: 10px;
    padding-right: 0px;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    color: #333;
    .info {
      margin-top: 5px;
      display: flex;
      justify-content: space-between;
      .price {
        color: #fa2209;
      }
    }
  }
}

.flow-num-box {
  display: flex;
  justify-content: flex-end;
  padding: 10px 10px;
  font-size: 14px;
  border-bottom: 1px solid #efefef;
  .money {
    color: #fa2209;
  }
}

.pay-cell {
  font-size: 14px;
  padding: 10px 12px;
  color: #333;
  display: flex;
  justify-content: space-between;
  .red {
    color: #fa2209;
  }
}
.pay-detail {
  border-bottom: 1px solid #efefef;
}

.pay-way {
  font-size: 14px;
  padding: 10px 12px;
  border-bottom: 1px solid #efefef;
  color: #333;
  .tit {
    line-height: 30px;
  }
  .pay-cell {
    padding: 10px 0;
  }
  .van-icon {
    font-size: 20px;
    margin-right: 5px;
  }
}

.buytips {
  display: block;
  textarea {
    display: block;
    width: 100%;
    border: none;
    font-size: 14px;
    padding: 12px;
    height: 100px;
  }
}

.footer-fixed {
  position: fixed;
  background-color: #fff;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 46px;
  line-height: 46px;
  border-top: 1px solid #efefef;
  font-size: 14px;
  display: flex;
  .left {
    flex: 1;
    padding-left: 12px;
    color: #666;
    span {
      color:#fa2209;
    }
  }
  .tipsbtn {
    width: 121px;
    background: linear-gradient(90deg,#f9211c,#ff6335);
    color: #fff;
    text-align: center;
    line-height: 46px;
    display: block;
    font-size: 14px;
  }
}
</style>